//
//  NSString+Date.m
//  ElectrocarShop
//
//  Created by hai on 2016/10/20.
//  Copyright © 2016年 hai. All rights reserved.
//

#import "NSString+Date.h"

@implementation NSString (Date)


#pragma mark date转字符串 格式：yyyy-MM-dd HH:mm:ss
+ (NSString *)dateToString:(NSDate *)date
{
    return [self dateToString:date DateFormatter:@"yyyy-MM-dd HH:mm:ss"];
}
#pragma mark date转字符串 格式：yyyy-MM-dd
+ (NSString *)onlyGetDateToString:(NSDate *)date
{
    return [self dateToString:date DateFormatter:@"yyyy-MM-dd"];
}
#pragma mark date转字符串 dateFormat:自定义转换格式
+ (NSString *)dateToString:(NSDate *)date DateFormatter:(NSString *)dateFormat
{
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:dateFormat];
    
    NSString *dateStr = [format stringFromDate:date];
    
    return dateStr;
}

#pragma mark 两个时间间隔 -- 刚刚  ？秒、分、小时、天、周、月、年 前
+ (NSString *)stringIntervalFrom:(NSDate *)start to:(NSDate *)end {
    NSInteger interval = end.timeIntervalSince1970 - start.timeIntervalSince1970;
    if (interval <= 0) {
        return @"刚刚";
    }
    
    if (interval < 60) {
        return [NSString stringWithFormat:@"%zd 秒前", interval];
    }
    
    interval = interval / 60;
    if (interval < 60) {
        return [NSString stringWithFormat:@"%zd 分钟前", interval];
    }
    
    interval = interval / 60;
    if (interval < 24) {
        return [NSString stringWithFormat:@"%zd 小时前", interval];
    }
    
    interval = interval / 24;
    if (interval < 7) {
        return [NSString stringWithFormat:@"%zd 天前", interval];
    }
    
    if (interval < 30) {
        return [NSString stringWithFormat:@"%zd 周前", interval / 7];
    }
    
    if (interval < 365) {
        return [NSString stringWithFormat:@"%zd 个月前", interval / 30];
    }
    return [NSString stringWithFormat:@"%zd 年前", interval / 365];
}


#pragma mark 根据生日计算年龄
+ (NSString *)ageFromBirthday:(NSString *)birthday
{
    return birthday;
}
+ (NSString *)ageFromBirthday:(NSString *)birthday DateFormatter:(NSString *)dateFormat
{
    
    return birthday;
}


#pragma mark 获取当前时间
+(NSString *)getCurrentTimeString{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate *datenow = [NSDate date];
    //将nsdate按formatter格式转成nsstring
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    return currentTimeString;
}

#pragma mark 获取指定时间时间之前N天的时间点
+(NSString *)getDate:(NSDate *)date daysAgoTimeStr:(NSString *)daysStr{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSTimeInterval timeInterval =  [daysStr intValue] * 24 * 60 * 60;
    NSDate *dateDaysAgo = [date dateByAddingTimeInterval:-timeInterval];
    NSString *daysAgoStr = [formatter stringFromDate:dateDaysAgo];
    return daysAgoStr;
}


#pragma mark 将时间戳转换为时间
+ (NSString *)transTimeStampToTime:(NSString *)timeStamp Format:(NSString *)format{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStamp doubleValue] / 1000];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];//HH:mm:ss
    NSString *dateStr = [dateFormatter stringFromDate:date];
    return dateStr;
}

#pragma mark 将时间转化为时间戳
+ (NSString *)transTimeToTimeStamp:(NSString *)time Format:(NSString *)format{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];//HH:mm:ss
    NSDate* dateBegain = [dateFormatter dateFromString:time];
    NSString *timeSp1 = [NSString stringWithFormat:@"%lld", (long long)([dateBegain timeIntervalSince1970] * 1000)];
    return timeSp1;
}


/**
 时间字符串 转 自定义时间格式字符串
 
 @param customFormat 自定义格式字符串 例如:@"HH:mm MM/dd"
 @param timeStr 原始时间格式字符串 这里必须是这个格式@"yyyy-MM-dd HH:mm:ss"要想是其他格式可在方法中进行更改
 @return 返回自定义格式字符串
 */
+ (NSString *)getCustomFormatTime:(NSString *)customFormat withTimeStr:(NSString *)timeStr{
    // 日期格式化类
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    // 设置日期格式(为了转换成功)
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    // NSString * -> NSDate *
    NSDate *date = [fmt dateFromString:timeStr];
    // NSDate * ->NSString *
    NSDateFormatter *customfmt = [[NSDateFormatter alloc] init];
    customfmt.dateFormat = customFormat;
    NSString *resultStr = [customfmt stringFromDate:date];
    
    return resultStr;
    
}



@end
